<template>
    <Header></Header>

    <!-- 文章详情 -->
    <div class="container mx-auto max-w-4xl mt-5 mb-3">
        <!-- 文章内容区域 - 全宽显示 -->
        <div class="px-3 mb-3">
            <div >
                <!-- 面包屑 -->
                <nav class="flex mb-6" aria-label="Breadcrumb">
                    <ol class="inline-flex items-center space-x-1 md:space-x-3">
                        <li class="inline-flex items-center">
                            <a @click="router.push('/')"
                                class="cursor-pointer inline-flex items-center text-sm font-medium text-gray-500 hover:text-blue-600 dark:text-gray-400 dark:hover:text-white transition-colors duration-200">
                                <svg class="w-3 h-3 mr-2.5" aria-hidden="true" xmlns="http://www.w3.org/2000/svg"
                                    fill="currentColor" viewBox="0 0 20 20">
                                    <path
                                        d="m19.707 9.293-2-2-7-7a1 1 0 0 0-1.414 0l-7 7-2 2a1 1 0 0 0 1.414 1.414L2 10.414V18a2 2 0 0 0 2 2h3a1 1 0 0 0 1-1v-4a1 1 0 0 1 1-1h2a1 1 0 0 1 1 1v4a1 1 0 0 0 1 1h3a2 2 0 0 0 2-2v-7.586l.293.293a1 1 0 0 0 1.414-1.414Z" />
                                </svg>
                                首页
                            </a>
                        </li>
                        <li aria-current="page">
                            <div class="flex items-center text-gray-400">
                                /
                                <span
                                    class="ml-1 text-sm font-medium text-gray-500 md:ml-4 dark:text-gray-400">正文</span>
                            </div>
                        </li>
                    </ol>
                </nav>


                <!-- 文章主体 -->
                <article class="max-w-none">
                    <h1 class="title mt-4 mb-6">{{ article.title }}</h1>
                    <div class="text-gray-400 text-sm flex flex-wrap items-center article-mata mb-8 gap-4">
                        <span class="flex items-center">
                            <svg t="1756950157238" class="inline w-4 h-4 mr-2 text-blue-500" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5224">
                                <path d="M512 64C264.58 64 64 264.58 64 512s200.58 448 448 448 448-200.58 448-448S759.42 64 512 64z m0 816c-203.24 0-368-164.76-368-368s164.76-368 368-368 368 164.76 368 368-164.76 368-368 368z" fill="currentColor" p-id="5225">
                                </path>
                                <path d="M688 472H552V272a40 40 0 0 0-80 0v240a40 40 0 0 0 40 40h176a40 40 0 0 0 40-40 40 40 0 0 0-40-40z" fill="currentColor" p-id="5226">
                                </path>
                            </svg>
                            发布时间 : {{ article.updateTime }}
                          </span>
<br/>
                        <span class="flex items-center">
                            <svg t="1756950404877" class="inline w-4 h-4 mr-2 text-green-500" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="6326">
                                <path d="M575.68 736a160.32 160.32 0 1 0 160.32-160.32H599.893333c-13.461333 0-24.192 10.752-24.192 24.192V736zM736 533.333333A202.666667 202.666667 0 1 1 533.333333 736V599.893333A66.432 66.432 0 0 1 599.872 533.333333H736zM490.666667 736a202.666667 202.666667 0 1 1-202.666667-202.666667h136.128A66.432 66.432 0 0 1 490.666667 599.872V736z m-202.666667-160.32a160.32 160.32 0 1 0 160.32 160.32V599.893333c0-13.44-10.730667-24.192-24.192-24.192H288zM533.333333 287.978667A202.666667 202.666667 0 1 1 736 490.666667H599.893333A66.432 66.432 0 0 1 533.333333 424.128V288z m202.666667 160.341333a160.32 160.32 0 1 0-160.32-160.32v136.128c0 13.44 10.730667 24.192 24.192 24.192H736zM448.32 288a160.32 160.32 0 1 0-160.32 160.32h136.128c13.461333 0 24.192-10.752 24.192-24.192V288zM288 490.666667A202.666667 202.666667 0 1 1 490.666667 288v136.128A66.432 66.432 0 0 1 424.128 490.666667H288z" fill="currentColor" p-id="6327"></path>
                            </svg>
                            所属分类 :&nbsp;<a @click="goCatagoryArticleListPage(article.categoryId, article.categoryName)"
                                class="text-gray-500 hover:text-blue-600 hover:underline transition-colors duration-200">{{ article.categoryName }}</a>
                        </span>
<br/>
                        <span class="flex items-center">
                            <svg t="1756950582910" class="inline w-4 h-4 mr-2 text-purple-500" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="7348">
                                <path d="M512 928c-185.28 0-336-148.352-336-330.688 0-99.04 28.8-169.6 96.256-235.968 12.96-12.768 27.552-25.504 42.752-38.72 59.552-52 121.12-105.728 121.12-194.656 0-11.808 6.592-22.656 17.184-28.192a33.056 33.056 0 0 1 33.376 1.6c2.592 1.696 234.016 157.76 137.024 426.72 19.84-14.816 39.36-34.656 53.088-60.672 12.896-24.576 19.456-52.672 19.456-83.424a32 32 0 0 1 20.192-29.6 32.96 32.96 0 0 1 35.52 7.168c61.056 60.992 96.032 146.976 96.032 235.776C848 779.648 697.28 928 512 928z" fill="currentColor" p-id="7349"></path>
                            </svg>
                            阅读量 : {{ article.readNum }}
                        </span>
                    </div>
                    <!-- 文章正文内容 -->
                    <div class="article-content prose prose-lg max-w-none p-0" v-viewer v-html="article.content" v-highlight>
                    </div>
                    
                    <!-- 标签 -->
                    <div class="mt-8 mb-8 pt-6 border-t border-gray-200 dark:border-gray-700">
                        <h3 class="text-lg font-semibold text-gray-900 dark:text-white mb-4">文章标签</h3>
                        <div class="flex flex-wrap gap-2">
                            <div @click="goTagArticleListPage(item.id, item.name)" v-for="(item, index) in article.tags"
                                :key="index"
                                class="inline-flex items-center px-3 py-1 rounded-full bg-blue-100 text-blue-800 text-sm font-medium hover:bg-blue-200 hover:text-blue-900 dark:bg-blue-900 dark:text-blue-300 dark:hover:bg-blue-800 dark:hover:text-blue-200 transition-colors duration-200 cursor-pointer">
                                <svg class="w-3 h-3 mr-1" fill="currentColor" viewBox="0 0 20 20">
                                    <path fill-rule="evenodd" d="M17.707 9.293a1 1 0 010 1.414l-7 7a1 1 0 01-1.414 0l-7-7A.997.997 0 012 10V5a3 3 0 013-3h5c.256 0 .512.098.707.293l7 7zM5 6a1 1 0 100-2 1 1 0 000 2z" clip-rule="evenodd" />
                                </svg>
                                {{ item.name }}
                            </div>
                        </div>
                    </div>
                </article>

                    <!-- 上下篇导航 -->
                <div class="pt-6 border-t border-gray-200 dark:border-gray-700">
                    <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                        <!-- 上一篇 -->
                        <div v-if="article.preArticleId" @click="goArticleDetail(article.preArticleId)"
                            class="cursor-pointer p-4 rounded-lg bg-gray-50 dark:bg-gray-800 hover:bg-gray-100 dark:hover:bg-gray-700 transition-colors duration-200 border border-gray-200 dark:border-gray-700 flex items-center justify-center">
                            <div class="text-center">
                                <span class="before-content">上一篇</span>
                                <span class="font-medium text-gray-800 dark:text-gray-200 line-clamp-1">{{ article.preArticleTitle }}</span>
                            </div>
                        </div>
                        <div v-else class="hidden md:block"></div>
                        <!-- 下一篇 -->
                        <div v-if="article.nextArticleId" @click="goArticleDetail(article.nextArticleId)"
                            class="cursor-pointer p-4 rounded-lg bg-gray-50 dark:bg-gray-800 hover:bg-gray-100 dark:hover:bg-gray-700 transition-colors duration-200 border border-gray-200 dark:border-gray-700 flex items-center justify-center">
                            <div class="text-center">
                                <span class="before-content">下一篇</span>
                                <span class="font-medium text-gray-800 dark:text-gray-200 line-clamp-1">{{ article.nextArticleTitle }}</span>
                            </div>
                        </div>
                    </div>
                </div>
                
                <!-- 评论区 -->
                <Comment 
                  ref="commentRef"
                  :article-id="articleId"
                  class="mt-8"
                />
            </div>
        </div>
    </div>

    <Footer></Footer>
</template>

<script setup>
import Header from '@/layouts/components/Header.vue'
import Footer from '@/layouts/components/Footer.vue'
import Comment from '@/components/Comment.vue'
import { useRoute, useRouter } from 'vue-router';
import { getArticleDetail } from '@/api/frontend/article';
import { ref, reactive, watch } from 'vue'
import { getCategories } from '@/api/frontend/category'
import { getTags } from '@/api/frontend/tag'

const router = useRouter()
const route = useRoute()
const commentRef = ref(null)
const articleId = ref(route.query.articleId)

const article = reactive({
    title: '',
    content: '',
    updateTime: '',
    readNum: 0,
    categoryId: null,
    categoryName: '',
    preArticleId: null,
    preArticleTitle: '',
    nextArticleId: null,
    nextArticleTitle: '',
    tags: [],
})

function queryArticleDetail(articleId) {
    // console.log('调用获取详情接口...' + articleId)
    getArticleDetail(articleId).then((e) => {
        article.title = e.data.title
        article.content = e.data.content
        article.updateTime = e.data.updateTime
        article.categoryId = e.data.categoryId
        article.categoryName = e.data.categoryName
        article.readNum = e.data.readNum
        article.tags = e.data.tags
        if (e.data.preArticle) {
            // console.log('上一篇...')
            // console.log(e.data.preArticle)
            article.preArticleId = e.data.preArticle.id
            article.preArticleTitle = e.data.preArticle.title
        } else {
            article.preArticleId = null
        }

        if (e.data.nextArticle) {
            article.nextArticleId = e.data.nextArticle.id
            article.nextArticleTitle = e.data.nextArticle.title
        } else {
            article.nextArticleId = null
        }
        
    })
}

queryArticleDetail(route.query.articleId);

// 监听路由变化，当查询参数改变时重新加载文章详情
watch(() => route.query.articleId, (newArticleId) => {
    if (newArticleId) {
        queryArticleDetail(newArticleId)
    }
})

const goArticleDetail = (articleId) => {
    console.log('跳转详情页' + articleId)
    router.push({ path: '/article/detail', query: { articleId: articleId } })
}

// 获取分类
const categories = ref([])
getCategories().then((e) => {
    console.log('获取分类数据')
    console.log(e)
    categories.value = e.data
})


// 获取标签
const tags = ref([])
getTags().then((e) => {
    console.log('获取标签数据')
    console.log(e)
    tags.value = e.data
})

const goCatagoryArticleListPage = (id, name) => {
    router.push({ path: '/category/list', query: { id: id, name: name } })
}

const goTagArticleListPage = (id, name) => {
    router.push({ path: '/tag/list', query: { id: id, name: name } })
}

</script>

<style scoped>
.before-content{
    text-align: center ;
    font-size: 30px !important;
}
.article-content img {
    border-radius: 8px;
}

@media (max-width: 768px) {
    .article-mata {
        flex-direction: column;
        gap: 8px !important;
    }
}

.title {
    padding-bottom: 20px;
    margin-bottom: 0;
    line-height: 1.3;
    word-wrap: break-word;
    font-size: 36px;
    font-weight: 700;
    color: #292525;
    text-align: center;
}

.dark .title {
    color: #ffffff;
}

.article-mata {
    margin-bottom: 30px;
    justify-content: center;
}

/* 文章内容区域样式优化 */
:deep(.article-content) {
    font-size: 18px; /* 增大文字大小 */
    line-height: 1.8;
    max-width: none;
    margin: 0 auto;
    padding: 0; /* 确保没有内边距 */
    border: none; /* 确保没有边框 */
}

:deep(pre) {
    background: #21252b;
    color: #f8f8f2;
    border-radius: 5px;
    padding: 10px 0 0;
    font-size: 17px;
    padding-left: 15px;
    border: none; 
}

:deep(pre code.hljs) {
    display: block;
    overflow-x: auto;
    padding: 1em;
    padding-left: 0!important;
    padding-top: 25px!important;
}

:deep(pre:before) {
    background: #fc625d;
    border-radius: 50%;
    box-shadow: 20px 0 #fdbc40, 40px 0 #35cd4b;
    content: ' ';
    height: 10px;
    margin-top: 5px;
    position: absolute;
    width: 10px;
}

:deep(.article-content p) {
    letter-spacing: 0.3px;
    margin: 0 0 20px 0;
    line-height: 32px; /* 增加行高以提高可读性 */
    color: #8895A2;
    font-weight: 400;
    word-break: normal;
    word-wrap: break-word;
    font-family: -apple-system, BlinkMacSystemFont, PingFang SC, Hiragino Sans GB, Microsoft Yahei, Arial, sans-serif;
    font-size: 25px !important; 
}

:deep(.article-content h1, .article-content h2, .article-content h3, .article-content h4, .article-content h5, .article-content h6)  {
    margin: 30px 0 10px 0;
    color: #292525;
    line-height: 150%;
    font-family: PingFang SC,Helvetica Neue,Helvetica,Hiragino Sans GB,Microsoft YaHei,"\5FAE\8F6F\96C5\9ED1",Arial,sans-serif;
}

:deep(.article-content h3) {
    font-size: 30px;
    margin-top: 40px;
    margin-bottom: 16px;
    font-weight: 600;
}

:deep(.image-caption) {
    min-width: 20%;
    max-width: 80%;
    min-height: 43px;
    display: block;
    padding: 10px;
    margin: 0 auto;
    /* border-bottom: 1px solid #eee; */
    font-size: 13px;
    color: #999;
    text-align: center;
}

:deep(code:not(pre code)) {
    padding: 2px 4px;
    margin: 0 2px;
    font-size: 95%!important;
    border-radius: 4px;
    color: rgb(41, 128, 185);
    background-color: rgba(27, 31, 35, 0.05);
    font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;
}

:deep(pre code) {
    display: block;
    font-size: 95% !important;
    background-color: rgba(27, 31, 35, 0.05);
    font-family: Operator Mono, Consolas, Monaco, Menlo, monospace;
    /* color: #fff; */
}

:deep(article ul) {
    padding-left: 40px;
}

:deep(article ul li) {
    list-style-type: disc;
    padding-top: 5px;
    padding-bottom: 5px;
    font-size: 16px;
}

:deep(blockquote) {
    /* margin: 20px 0; */
    border-left: 2.3px solid rgb(52, 152, 219);
    quotes: none;
    background: rgb(236, 240, 241);
    color: #777;
    font-size: 16px;
    /* padding: 10px 15px 10px 15px; */
    margin: 2em 0;
    padding: 24px 24px;
    position: relative;
}

:deep(blockquote p:last-child) {
    margin-bottom: 0;
}

:deep(table tr) {
    background-color: #fff;
    border-top: 1px solid #c6cbd1;
}

:deep(table) {
    border-collapse: collapse;
    margin-bottom: 1rem;
}

:deep(table th) {
    padding: 6px 13px;
    border: 1px solid #dfe2e5;
}

:deep(table td) {
    padding: 6px 13px;
    border: 1px solid #dfe2e5;
}

:deep(.article-content a) {
    color: #167bc2;
}

:deep(.article-content h2) {
    /* margin: 1em auto; */
    font-size: 22px;
    line-height: 1.5;
    font-weight: bold;
    font-synthesis: style;
    /* border-bottom: 1px solid rgba(0,0,0,.1); */
    padding-bottom: 16px;
    /* border-left: 3px solid #167bc2; */
    padding-bottom: 0;
    font-size: 24px;
    margin-top: 40px;
    margin-bottom: 26px;
    line-height: 140%;
    border-bottom: 1px solid #e5e5e5;
    padding-bottom: 15px;
}

:deep(.article-content svg) {
    display: inline;
}

:deep(.article-content img) {
    position: relative;
    max-width: 100%;
    overflow: hidden;
    display: block;
    margin: 0 auto;
    cursor: -webkit-zoom-in;
    cursor: zoom-in;
}

:deep(strong) {
    color: rgb(52, 152, 219);
}

:deep(table tr:nth-child(2n)) {
    background-color: #f6f8fa;
}

.el-breadcrumb__inner a {
    font-weight: 400;
    color: #606266;
}

.el-breadcrumb__inner a:hover {
    font-weight: 400;
    color: #606266;
    text-decoration: underline;
}

.article-footer {
    border-top: 1px solid #e4e7ed;
    background-color: var(--el-fill-color-blank);
    justify-content: space-between;
    padding-top: 1rem;
}

.desc {
    display: block;
    font-size: 12px;
    color: rgba(60, 60, 60, .7);
}

.cursor-pointer {
    cursor: pointer;
}

.category-item:hover {
    text-decoration: underline;
    cursor: pointer;
}

.tag-item:hover {
    cursor: pointer;
}

.el-tag:hover {
    background-color: var(--el-color-info-light-8);
}
</style>